package design.factory;

/**
 * 简单工厂模式
 */

// 抽象产品类 方法是抽象
interface Product {
    void operation();
}

class ConcreteProductA implements Product {
    public void operation()
    {
        System.out.println("ConcreteProductA");
    }
}

class ConcreteProductB implements Product {
    public void operation()
    {
        System.out.println("ConcreteProductB");
    }
}

// 生产商品的工厂 通过名字确定生产什么
class Factory {
    public Product CreateProduct(String type) {
        if(type == null || type.isEmpty()) return null;
        if (type.equals("A"))
        {
            return new ConcreteProductA();
        }
        else if (type.equals("B"))
        {
            return new ConcreteProductB();
        }
        return null;
    }
}

public class SimpleFactory {
    public static void main(String[] args) {
        Factory factory = new Factory();
        Product a = factory.CreateProduct("A");
        a.operation();
        Product b = factory.CreateProduct("B");
        b.operation();
    }
}
